<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class address_uqcms extends control
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        parent::__construct();
    }
    public function get_one($id)
    {
        return $this->module('address')->get_one($id);
    }
    public function edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->module('address')->get_one($id);
            $this->assign('row', $row);
        }
        $this->display();
    }
    function edit_save()
    {
        if ($_POST) {
            $prov = $dists[] = isset($_POST['prov']) ? $_POST['prov'] : '';
            $city = $dists[] = isset($_POST['city']) ? $_POST['city'] : '';
            $dist = $dists[] = isset($_POST['dist']) ? $_POST['dist'] : '';
            $dist_result = $this->module('address')->get_cn($dists);
            $setarr['prov'] = $dist_result[$prov]['id'];
            $setarr['prov_cn'] = $dist_result[$prov]['name'];
            $setarr['city'] = isset($dist_result[$city]['id']) ? $dist_result[$city]['id'] : '0';
            $setarr['city_cn'] = isset($dist_result[$city]['name']) ? $dist_result[$city]['name'] : '';
            $setarr['dist'] = isset($dist_result[$dist]['id']) ? $dist_result[$dist]['id'] : '0';
            $setarr['dist_cn'] = isset($dist_result[$dist]['name']) ? $dist_result[$dist]['name'] : '';
            $setarr['name'] = !empty($_POST['name']) ? $_POST['name'] : alert('姓名不能为空');
            $setarr['mobile'] = !empty($_POST['mobile']) ? $_POST['mobile'] : alert('电话不能为空');
            $setarr['address'] = !empty($_POST['address']) ? trim($_POST['address']) : alert('地址不能为空');
            $setarr['zip'] = !empty($_POST['zip']) ? trim($_POST['zip']) : '';
            $setarr['addtime'] = time();
            $id = isset($_POST['id']) ? $_POST['id'] : '';
            if ($id) {
                $query = $this->db->update(table('member_address'), $setarr, 'uid = ' . $this->uid . ' and id = ' . $id);
            } else {
                if (cfg('sys_settle_login') == '1') {
                    $address_num = cfg('user_address_num') ? cfg('user_address_num') : '20';
                    if ($address_num) {
                        $addnum = $this->db->get_one("select count(id) as total from " . table('member_address') . " where uid = " . $this->uid);
                        if ($addnum['total'] >= $address_num) {
                            error_json('已经超出最大的数量');
                        }
                    }
                    $setarr['uid'] = $this->uid;
                } else {
                    $setarr['uid'] = '0';
                }
                $query = $this->db->add(table('member_address'), $setarr);
                $id = $this->db->insert_id();
            }
            if (!empty($_POST['is_default'])) {
                $this->set_default_func($id);
            }
            if ($query) {
                $result = $setarr;
                $result['id'] = $id;
                $result['error'] = '0';
                $result['msg'] = '保存成功';
                echo json_encode($result);
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function del()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : error_json('提交错误');
        $query = $this->db->delete(table('member_address'), 'uid = ' . $this->uid . ' and id = ' . $id);
        if ($query) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
    function set_default()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : error_json('访问错误');
        $query = $this->set_default_func($id);
        if ($query) {
            right_json();
        } else {
            error_json('设置失败');
        }
    }
    function set_default_func($id)
    {
        $this->db->autocommit();
        $query1 = $this->db->update(table('member_address'), array('is_default' => '0'), 'uid = ' . $this->uid);
        $query2 = $this->db->update(table('member_address'), array('is_default' => '1'), 'uid = ' . $this->uid . ' and id = ' . $id);
        if ($query1 && $query2) {
            $this->db->commit();
            return true;
        } else {
            $this->db->rollback();
            return false;
        }
    }
}